import modelExtend from 'dva-model-extend'
import * as friendStatisticsService from './services/friendStatistics'
import * as dictionaryService from 'services/dictionary'
import * as employeeService from "../employee/services/employees"
import * as departmentService from '../department/services/departments'
import { pageModel } from 'utils/model'
const { query, queryStatisticalDiagram, getChinaMapData } = friendStatisticsService

export default modelExtend(pageModel, {
  namespace: 'friendStatistics',
  state: {
    condition: 'wxId',    //条件搜索
    adAcquireType: [],
    empList: [],
    departments: [],
    tabsKey: 'table_form',
    dataOfDiagram: [],
    chinaMapData: [],
    start: null,
    end: null,
    filter: {}
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/friendStatistics') {
          const payload = location.query || { current: 1, size: 10 }
          dispatch({
            type: 'query',
            payload,
          })
          dispatch({
            type: 'init'
          })
        }
      })
    },
  },
  effects: {
    * query({ payload = {} }, { call, put }) {
      const response = yield call(query, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    *queryStatisticalDiagram({ payload = {} }, { call, put }) {

      const response = yield call(queryStatisticalDiagram, payload)
      const { data } = response
      console.log(data.accountByGenderVOS.length == 0)
      if (response && data) {
        console.log(data)
        yield put({
          type: 'updateState',
          payload: {
            dataOfDiagram: data.accountByGenderVOS.length == 0 ? (data.accountByAddTypeVOS.length == 0 ? data.accountByAdAcquireVOS : data.accountByAddTypeVOS) : data.accountByGenderVOS
          },
        })
      }
    },

    *getChinaMapData({ payload = {} }, { call, put }) {
      const response = yield call(getChinaMapData, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            chinaMapData: data.records
          },
        })
      }
    },

    * init({ payload = {} }, { call, put }) {
      const adAcquireTypeResp = yield call(dictionaryService.queryBySn, { sn: 'customer_channel' })
      const deptResp = yield call(departmentService.query)

      yield put({
        type: 'updateState', payload: {
          adAcquireType: adAcquireTypeResp.data,
          departments: deptResp.data,
        },
      })
    },

    * searchEmp({ payload = {} }, { call, put }) {
      const response = yield call(employeeService.searchEmp, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empList: data,
          },
        })
      }
    },

  },

  reducers: {
    updateCondition(state, { payload }) {
      return { ...state, ...payload }
    },


    updateTabsKey(state, { payload }) {
      return { ...state, ...payload }
    }
  }
})
